; (function ($) {
	$.ui = {
		validate:function(f1){
			let fd = new FormData(f1);
			for (let k of fd.keys()) {
				if (f1[k].checkValidity()) {
				   	$(f1[k]).parents('.form-group').removeClass('has-error');
				} else {
					$(f1[k]).parents('.form-group').addClass('has-error');
				}
			}
		   return f1.checkValidity();
		},
		menus:[{
			'title':'Demo', 
			'isTree':true, 
			'icon':'fa fa-th', 
			'nodes':[
				{'title':'首页', 'url':'/index.html'},
				{'title':'布局','url':'/layout.html'}]
			},{
				'title':'Documentation',
				'icon':'fa fa-book',
				'url':'/'
			}
		],
		buildMenu:function(){
			let s = $('.sidebar-menu');
			if (!s) {
				return;
			}

			let str ='';
			for (let i = 0; i < this.menus.length; i++) {
				let m = this.menus[i];
				if (m.isTree) {
					str += '<li class="treeview"><a href="#" ><i class="'+ m.icon +'">'+'</i><span>'+ m.title +'</span></a>';
					str += '<ul class="treeview-menu">';
					for (let j = 0; j < m.nodes.length; j++) {
						let n = m.nodes[j];
						str += '<li><a href="' + n.url +'"><i class="fa fa-circle-o"></i>'+ n.title +'</a></li>';
					}
					str += '</ul></li>';
				}
				else{
					str += '<li><a href="'+ m.url +'"><i class="'+ m.icon +'"></i><span>'+ m.title +'</span></a><li>';
				}
			}
			s.html(str);
			//console.log(str);
			let links = s.find('a');
			let link = null;
			let pathname = location.pathname;
			for (let i = 0; i < links.length; i++) {
				let l = links[i];
				let href = $(l).attr('href');
				if (href == pathname) {
					link = l;
					break;
				}
			}
			if (link) {				
				//console.log(link);
				$(link).parents('li').addClass('active');
			}
		},
		dialog:function( options ){
			let s = 'd'+ Date.now();
			let de = {
				title:'&nbsp;',
				class:'',
				render:function(){return '<div><div>';},
				rendered: function(e) {}
			};
			let settings = $.extend({}, de, options);
			var htmlstr='<div id="'+ s +'"class="modal show" tabindex="-1" >'
			+'<div class="modal-dialog '+ settings.class +'"><div class="modal-content"><div class="modal-header">'
			+'<button type="button" class="close" >&times;</button><div class="modal-title">'+ (settings.title|| ' ') +'</div>'
			+'</div><div class="modal-body"></div><div class="modal-footer">'
			+'<button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button>'
			+'<button type="button" class="btn btn-primary">确定</button></div></div></div></div>';
			$('.content:last').append(htmlstr);
			let element = $('#'+s);
			element.find('.modal-body').html(settings.render());
			settings.rendered(element);
			let d = {
				'id': s,
				'element':element,
				'show':function(){
					this.element.addClass('show');
				},
				'hide':function(){
					this.element.removeClass('show');
				},
				'remove':function(){
					this.element.remove();
				}
			};
			element.find('.close').click(function(){
				d.remove();
			});
			let btns = element.find('.modal-footer>button');
			btns[1].click = settings.ok;
			$(btns[0]).click(function(){
				if (settings.cancle) {
					settings.cancle();
				}
				d.remove();
			});
			$(btns[1]).click(function(){
				if (settings.ok) {
					settings.ok();
				}
			});
			return d;
		},
		layout:function(){
			let body = $('body');
			body.addClass('skin-blue');
			body.addClass('sidebar-mini');
			let content = $('.content-wrapper:last');
			if (!content) {
				return;
			}
			let wrapper = $('.wrapper');
			if (wrapper.length > 0) {
				return;
			}
			body.append('<div class="wrapper" style="height: auto;"><header class="main-header"><a href="#" class="logo">'
				+'<span class="logo-mini">标</span><span class="logo-lg">Logo</span></a><nav class="navbar navbar-static-top">'
				+'<a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button"><span class="sr-only">Toggle navigation</span>'
				+'<span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></a><div class="navbar-custom-menu">'
				+'</div></nav></header><aside class="main-sidebar"><section class="sidebar"><div class="user-panel"></div>'
				+'<ul class="sidebar-menu"></ul></section></aside></div><footer class="main-footer"></footer></div>');

			wrapper = $('.wrapper:first');
			wrapper.append(content);
		},
		notice: function(options) {
            //let id = 'n999999999';
            let de = {
                parentId: 'n999999999',
                text: '&nbsp;',
                type: 'info',
                autoClose: true,
                seconds: 10
            }
            let settings = $.extend({}, de, options);
            let container = $("#n999999999");
            if (!container.length > 0) {
                $("body").append("<div id='n999999999' style='position:absolute; top:10px; right:10px; width:300px;z-index:1200;overflow:hidden;'></div>");
                container = $("#n999999999");
            }
            let cls = 'alert-info';
            if (settings.type === 'warning') {
                cls = 'alert-warning';
            }
            if (settings.type === 'error') {
                cls = 'alert-error';
            }
            if (settings.type === 'ok') {
                cls = 'alert-success';
            }
            let id = 'nt' + Date.now();
            container.prepend("<div id='" + id + "' style='margin-top:-10px; margin-left:300px;' class='alert " + cls + " alert-dismissible'>"
                + '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'
                + settings.text + "</div>");
            $("#" + id).animate({ 'margin-top': 0, 'margin-left': 0 });
            setTimeout('$("#' + id + '").animate({opacity:0}, function(){$("#' + id + '").remove();});', settings.seconds * 1000);
        }
	}
})(jQuery);